package com.ningxun.rms.web.controller.usersignup;

import com.ningxun.rms.common.annotation.Anonymous;
import com.ningxun.rms.common.core.controller.BaseController;
import com.ningxun.rms.common.core.domain.AjaxResult;
import com.ningxun.rms.common.utils.SecurityUtils;
import com.ningxun.rms.domain.basewechatuser.BaseWechatUser;
import com.ningxun.rms.domain.busrequire.BusRequire;
import com.ningxun.rms.service.baserequire.IBusRequireService;
import com.ningxun.rms.service.userservice.IBaseWechatUserService;
import com.ningxun.rms.signup.domain.BusSignup;
import com.ningxun.rms.signup.mapper.BusSignupMapper;
import com.ningxun.rms.signup.service.IBusSignupService;
import com.ningxun.rms.web.controller.usersignup.vo.SignUpSuccessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import static com.ningxun.rms.common.core.domain.AjaxResult.error;

@RestController
@Anonymous
@RequestMapping
public class UserSignUpController extends BaseController {
    @Autowired
    private IBusRequireService busRequireService;
    //报名
    @Autowired
    private IBusSignupService busSignupService;

    @Autowired
    private IBaseWechatUserService baseWechatUserService;
    @Autowired
    private BusSignupMapper busSignupMapper;

    //点击某个需求报名来检查是否报名已满并且是否需要自动填充
    @GetMapping("/link/signup/{requireId}")
    public AjaxResult linkSignUp(@PathVariable("requireId") Long requireId) {
        if (SecurityUtils.getUserId() == null) {
            return error("请先登录");
        }
        Long userId = SecurityUtils.getUserId();
        BusRequire busRequire = busRequireService.selectBusRequireByRequireId(requireId);
        if (busRequire.getPost() == null||busRequire.getRequireState() != 0) {
            return error("该岗位不存在或没通过审核");
        }
        if (busRequire.getQuantity() <= 0) {
            return error(busRequire.getPost() + "岗位已满员");
        }
        BusSignup busSignupLately = busSignupMapper.selectSignupLatelyByUserId(SecurityUtils.getUserId());
        if (busSignupLately != null) {
            return new AjaxResult(200, "1", busSignupLately);
        } else {
            return new AjaxResult(200, "新用户，自己填");
        }

    }

    //填写报名信息并报名
    @PostMapping("/signup")
    public AjaxResult signUp(@RequestBody BusSignup busSignup) {
        System.out.println(SecurityUtils.getUserId());
        System.out.println(SecurityUtils.getUsername());
        BusRequire busRequire = busRequireService.selectBusRequireByRequireId(busSignup.getRequireId());
        if (busRequire == null||busRequire.getRequireState() != 0) {
            return error("该岗位不存在或没通过审核");
        }
        if (busRequire.getQuantity() <= 0) {
            return AjaxResult.warn("该" + busRequire.getPost() + "报名通过已满");
        }
        busSignup.setUserId(SecurityUtils.getUserId());
        BusSignup busSignupLately = busSignupMapper.selectSignupLatelyByUserId(SecurityUtils.getUserId());
        if (busSignupLately == null || busSignupLately.getApproveStatus() == 2) {
            return toAjax(busSignupService.insertBusSignup(busSignup));
        } else if(busSignupLately.getApproveStatus() == 0){
            Long requireIdLately = busSignupLately.getRequireId();
            BusRequire busRequireLately = busRequireService.selectBusRequireByRequireId(requireIdLately);
            return AjaxResult.error(500,busRequireLately.getPost() + "岗位申请中，请勿再次报名");
        }else{
            Long requireIdLately = busSignupLately.getRequireId();
            BusRequire busRequireLately = busRequireService.selectBusRequireByRequireId(requireIdLately);
            return AjaxResult.error(500,busRequireLately.getPost() + "岗位申请通过，无需再次报名");
        }

    }

    //查看是否成功
    @GetMapping("/signup/info")
    @Scheduled(cron = "*/5 * * * * ?")
    public AjaxResult isSuccess() {
        String openId = SecurityUtils.getUsername();
        Integer integer = busSignupService.selectIsSuccess(openId);
        BusSignup busSignupLately = busSignupMapper.selectSignupLatelyByUserId(SecurityUtils.getUserId());
        if (integer == 1 || integer == 2) {
            if (busSignupLately != null) {
                if (busSignupLately.getApproveStatus() == 1) {
                    SignUpSuccessVO signUpSuccessVO = SignUpSuccessVO.builder()
                            .status(1L)
                            .post(busRequireService.selectBusRequireByRequireId(busSignupLately.getRequireId()).getPost())
                            .signUpId(busSignupLately.getSignupId())
                            .build();
                    return success(signUpSuccessVO);
                } else if (busSignupLately.getApproveStatus() == 2) {
                    SignUpSuccessVO signUpSuccessVO = SignUpSuccessVO.builder()
                            .status(2L)
                            .post(busRequireService.selectBusRequireByRequireId(busSignupLately.getRequireId()).getPost())
                            .signUpId(busSignupLately.getSignupId())
                            .build();
                    return success(signUpSuccessVO);
                       }
            }
        }else if (integer == 0) {
            SignUpSuccessVO signUpSuccessVO = SignUpSuccessVO.builder()
                    .status(0L)
                    .post(busRequireService.selectBusRequireByRequireId(busSignupLately.getRequireId()).getPost())
                    .signUpId(busSignupLately.getSignupId())
                    .build();
            return success(signUpSuccessVO);
        }
        SignUpSuccessVO signUpSuccessVO = SignUpSuccessVO.builder()
                .status(-1L)
                .post(null)
                .signUpId(null)
                .build();
        return success(signUpSuccessVO);
    }
    //查看报名信息根据主键id
    @GetMapping("/{signupId}")
    public AjaxResult selectSignupById(@PathVariable("signupId") Long signupId) {
        BusSignup busSignup = busSignupService.selectBusSignupBySignupId(signupId);
        return success(busSignup);
    }

}
